package Media 
{
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.TextInput;
	import feathers.display.Scale9Image;
	import feathers.skins.Scale9ImageStateValueSelector;
	import feathers.themes.MetalWorksMobileTheme;
	import flash.text.TextFormat;
    import starling.display.DisplayObjectContainer;
	import starling.display.Quad;
	
	/**
	 * @author Simon Claeye
	 */
	public class ExtendedMetalWorksMobileTheme extends MetalWorksMobileTheme
	{
		//Names - button
		public static const MENU_BUTTON:String = "Menu Button";
		
		//Names -Input label
		public static const INPUT_LABEL:String = "Input Label";
		
		private var menuButtonFormat:TextFormat;
		private var menuButtonDisabledUIFormat:TextFormat;
		private var inputSmallLightTextFormat:TextFormat;
		private var tabButtonLabelFormat:TextFormat;
		private var tabButtonFormat:TextFormat;
		private var tabButtonDisabledFormat:TextFormat;
		
		public function ExtendedMetalWorksMobileTheme( root:DisplayObjectContainer, scaleToDPI:Boolean = true )
		{
			super( root, scaleToDPI );
			
			//Text Formats
			this.menuButtonFormat = super.largeUIDarkTextFormat;
			this.menuButtonFormat.size = 40 * this.scale;
			
			this.menuButtonDisabledUIFormat = super.largeUIDisabledTextFormat;
			this.menuButtonDisabledUIFormat.size = 40 * this.scale;
			
			this.inputSmallLightTextFormat = super.smallLightTextFormat;
			this.inputSmallLightTextFormat.size = 40 * this.scale;
			this.tabButtonFormat = super.smallUIDarkTextFormat;
			this.tabButtonFormat.size = 30 * this.scale;
			this.tabButtonLabelFormat = super.smallUILightTextFormat;
			this.tabButtonLabelFormat.size = 30 * this.scale;
			this.tabButtonDisabledFormat = super.smallUIDisabledTextFormat;
			this.tabButtonDisabledFormat.size = 30 * this.scale;
		}
 
		override protected function initialize():void
		{
			super.initialize();
 
			// set new initializers here
			this.setInitializerForClass( Button, myButtonInitializer, MENU_BUTTON );
			this.setInitializerForClass( Label, myLabelInitializer, INPUT_LABEL );
		}
		
		/**
		 * Custom factory class for creating buttons. Only changes the text format of the buttons, all other code
		 * is copied from the parent class.
		 * @param	button
		 */
		private function myButtonInitializer( button:Button ):void
		{
			//Use theme code
			var skinSelector:Scale9ImageStateValueSelector = new Scale9ImageStateValueSelector();
			skinSelector.defaultValue = this.buttonUpSkinTextures;
			skinSelector.defaultSelectedValue = this.buttonSelectedUpSkinTextures;
			skinSelector.setValueForState(this.buttonDownSkinTextures, Button.STATE_DOWN, false);
			skinSelector.setValueForState(this.buttonDisabledSkinTextures, Button.STATE_DISABLED, false);
			skinSelector.setValueForState(this.buttonSelectedDisabledSkinTextures, Button.STATE_DISABLED, true);
			skinSelector.imageProperties =
			{
				width: 60 * this.scale,
				height: 60 * this.scale,
				textureScale: this.scale
			};
			button.stateToSkinFunction = skinSelector.updateValue;

			//Use custom text format
			button.defaultLabelProperties.textFormat = this.largeUIDarkTextFormat;
			button.disabledLabelProperties.textFormat = this.largeUIDisabledTextFormat;
			button.selectedDisabledLabelProperties.textFormat = this.largeUIDisabledTextFormat;

			//Use theme code
			button.paddingTop = button.paddingBottom = 8 * this.scale;
			button.paddingLeft = button.paddingRight = 16 * this.scale;
			button.gap = 12 * this.scale;
			button.minWidth = button.minHeight = 60 * this.scale;
			button.minTouchWidth = button.minTouchHeight = 88 * this.scale;
		}
		
		/**
		 * Custom factory class for creating labels. Only changes the text format of the labels, all other code
		 * is copied from the parent class.
		 * @param	button
		 */
		private function myLabelInitializer( label:Label ):void
		{
			label.textRendererProperties.textFormat = this.smallLightTextFormat;
		}
		
		/**
		 * Custom factory class for creating tabs. Only changes the text format of the buttons, all other code
		 * is copied from the parent class.
		 * @param	button
		 */
		private function myTabInitializer(tab:Button):void
		{
			//Use theme settings
			var defaultSkin:Quad = new Quad(88 * this.scale, 88 * this.scale, 0x1a1a1a);
			tab.defaultSkin = defaultSkin;

			var downSkin:Scale9Image = new Scale9Image(this.tabDownSkinTextures, this.scale);
			tab.downSkin = downSkin;

			var defaultSelectedSkin:Scale9Image = new Scale9Image(this.tabSelectedSkinTextures, this.scale);
			tab.defaultSelectedSkin = defaultSelectedSkin;
			
			//Custom text formats
			tab.defaultLabelProperties.textFormat = this.tabButtonLabelFormat;
			tab.defaultSelectedLabelProperties.textFormat = this.tabButtonFormat;
			tab.disabledLabelProperties.textFormat = this.tabButtonDisabledFormat;
			tab.selectedDisabledLabelProperties.textFormat = this.tabButtonDisabledFormat;
			
			//Use theme settings
			tab.paddingTop = tab.paddingBottom = 8 * this.scale;
			tab.paddingLeft = tab.paddingRight = 16 * this.scale;
			tab.gap = 12 * this.scale;
			tab.minWidth = tab.minHeight = 88 * this.scale;
			tab.minTouchWidth = tab.minTouchHeight = 88 * this.scale;
		}
	}
}